<script setup lang="ts">
import axios from "axios";
import { ElMessage } from "element-plus";
import { onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { router } from "../../router";
import type { Book } from "../../types/Book";
import type { ResultContext } from "../../types/ResultContext";

const bookList = ref<Book[]>([]);
const route = useRoute();
// 获取路由查询参数中的 keyword
const keyword = ref((route.query.keyword as string) || "");

onMounted(async () => {
  try {
    const res = await axios.get<ResultContext<Book[]>>(
      "http://localhost:8080/api/books/search",
      {
        params: { keyword: keyword.value },
      },
    );
    // 这里直接用 res.data，因为后端直接返回 List<Book>
    bookList.value = res.data.data || [];
  } catch (_err) {
    ElMessage.error("获取搜索结果失败。");
  }
});
</script>

<template>
  <div class="search-result-wrapper">
    <!-- 顶部结果汇总 -->
    <div class="search-summary">
      <h2 class="summary-title">搜索结果</h2>
      <p class="summary-subtitle">共找到 {{ bookList.length }} 本书</p>
    </div>

    <!-- 图书卡片展示区域 -->
    <div v-if="bookList.length > 0" class="card-container">
      <div
        v-for="book in bookList"
        :key="book.id"
        class="book-card"
        @click="router.push(`/details/${book.id}`)"
      >
        <div class="cover-wrapper">
          <img :src="`/img/${book.img}`" alt="书籍封面" class="cover" />
        </div>
        <div class="info">
          <h3 class="book-title">{{ book.title }}</h3>
          <p class="book-author">{{ book.author }}</p>
          <p class="book-description">{{ book.description }}</p>
        </div>
      </div>
    </div>

    <!-- 无结果提示 -->
    <div v-else class="no-result">暂无相关书籍</div>
  </div>
</template>

<style scoped>
.search-result-wrapper {
  padding: 40px 0;
  max-width: 1440px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.search-summary {
  margin-bottom: 30px;
  width: 80%;
  background: #f0f7ff;
  border-radius: 16px;
  padding: 20px 32px;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.1);
  border: 1px solid #c6e2ff;
  box-sizing: border-box;
}

.summary-title {
  font-size: 22px;
  font-weight: 700;
  color: #409eff; /* Element Plus 主蓝色 */
  margin: 0;
}

.summary-subtitle {
  font-size: 14px;
  color: #666;
  margin-top: 8px;
}

.summary-title {
  font-size: 24px;
  font-weight: bold;
  color: #2d3748;
}

.summary-subtitle {
  font-size: 14px;
  color: #666;
  margin-top: 6px;
}

.card-container {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 24px;
  width: 80%;
  align-items: stretch;
}

.book-card {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
  padding: 12px;
  display: flex;
  flex-direction: column;
  transition: all 0.2s;
  cursor: pointer;
  height: 100%;
  box-sizing: border-box;
  min-height: 380px;
  max-height: 380px;
}

.book-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}

.cover-wrapper {
  width: 100%;
  height: 60%;
  overflow: hidden;
  border-radius: 12px;
  margin-bottom: 12px;
  flex-shrink: 0; /* 防止图片区域被压缩 */
}

.cover {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.info {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0; /* 修复flex布局溢出问题 */
}

.book-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.4;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
}

.book-author {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden; /* 必须加上 */
  text-overflow: ellipsis; /* 超出显示省略号 */
}

.book-description {
  font-size: 13px;
  color: #999;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 3; /* 简介只显示2行 */
  line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: auto; /* 确保简介在底部 */
}

.no-result {
  text-align: center;
  color: #999;
  font-size: 16px;
  padding: 60px 20px;
}

@media (max-width: 1200px) {
  .card-container {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 992px) {
  .card-container {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 600px) {
  .card-container {
    grid-template-columns: minmax(0, 1fr);
  }
  .search-result-wrapper {
    padding: 20px;
  }
  .search-summary {
    width: 100%;
    padding: 0 20px;
  }
}
</style>
